package com.cytech.cloudclinic.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.adapter.AddDrugTagAdapter;
import com.cytech.cloudclinic.base.BaseBean;
import com.cytech.cloudclinic.base.BaseObserver;
import com.cytech.cloudclinic.bean.AddDrugTag;
import com.cytech.cloudclinic.bean.Drug;
import com.cytech.cloudclinic.utils.ConvertUtils;
import com.cytech.cloudclinic.utils.RequestWithViewUtils;
import com.yechaoa.yutils.YUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_2;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_3;
import static com.cytech.cloudclinic.app.Constant.DRUG_TAG_4;

/**
 * 选择用法，天数，次数对话框
 */
public class SelectUseAndDayDialog extends DialogFragment {
    @BindView(R.id.ll_select_use)
    LinearLayout userLayout;
    @BindView(R.id.ll_select_day)
    LinearLayout dayLayout;
    @BindView(R.id.ll_select_day_times)
    LinearLayout timesLayout;

    @BindView(R.id.rv_select_use)
    RecyclerView useRv;
    @BindView(R.id.rv_select_day)
    RecyclerView daysRv;
    @BindView(R.id.rv_select_day_times)
    RecyclerView timesRv;

    @BindView(R.id.tv_prescription_child_drug_use)
    TextView tvUse;
    @BindView(R.id.tv_prescription_child_drug_day)
    TextView tvDay;
    @BindView(R.id.tv_prescription_child_drug_day_times)
    TextView tvTime;

    private List<AddDrugTag> useList;
    private List<AddDrugTag> dayList;
    private List<AddDrugTag> timesList;
    private AddDrugTagAdapter useAdapter;
    private AddDrugTagAdapter dayAdapter;
    private AddDrugTagAdapter timesAdapter;


    private int drugParentTagId;

    public SelectUseAndDayDialog(int drug_tag_id) {
        Bundle bundle = new Bundle();
        bundle.putInt("drug_tag_id",drug_tag_id);
        setArguments(bundle);
    }


    @Override
    public void onStart() {
        super.onStart();
        WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
        lp.width = (int) (0.7* YUtils.getScreenWidth());
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getDialog().getWindow().setAttributes(lp);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_select_use_and_day, null);
        ButterKnife.bind(this, view);
        builder.setView(view);

        drugParentTagId = getArguments().getInt("drug_tag_id");
        sureShowView();
        return builder.create();
    }


    @OnClick(R.id.dialog_update_unit_cancel)
    public void onClickCancel(View view){
        dismiss();
    }

    @OnClick(R.id.dialog_update_unit_save)
    public void onClickSave(View view){
        dismiss();
    }

    /**
     * 确定页面布局要显示哪些元素
     */
    private void sureShowView(){
        switch (drugParentTagId){
            case DRUG_TAG_2:
                userLayout.setVisibility(View.GONE);
                dayLayout.setVisibility(View.VISIBLE);
                timesLayout.setVisibility(View.GONE);
                initDayRv();
                break;
            case DRUG_TAG_3:
                userLayout.setVisibility(View.VISIBLE);
                dayLayout.setVisibility(View.VISIBLE);
                timesLayout.setVisibility(View.VISIBLE);
                initUseRv();
                initDayRv();
                initTimeRv();
                getSummaryList();
                break;
            case DRUG_TAG_4:
                userLayout.setVisibility(View.GONE);
                dayLayout.setVisibility(View.VISIBLE);
                timesLayout.setVisibility(View.VISIBLE);
                initDayRv();
                initTimeRv();
                break;
        }
    }

    //用法
    private void initUseRv() {
        useList = new ArrayList<>();
        useAdapter = new AddDrugTagAdapter(R.layout.item_add_drug_tag, useList);
        useRv.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        useRv.setAdapter(useAdapter);

        useAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                useAdapter.setCurrentPos(position);
                tvUse.setText(useAdapter.getItem(position).value);
            }
        });
    }

    //服用天数
    private void initDayRv() {
        dayList = buildNumValue();
        dayAdapter = new AddDrugTagAdapter(R.layout.item_add_drug_tag, dayList);
        daysRv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        daysRv.setAdapter(dayAdapter);

        dayAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                String value = dayAdapter.getItem(position).value;
                tvDay.setText(ConvertUtils.getRealNumValue(value, tvDay.getText().toString()));
            }
        });
    }

    //每日服用
    private void initTimeRv() {
        timesList = buildNumValue();
        timesAdapter = new AddDrugTagAdapter(R.layout.item_add_drug_tag, timesList);
        timesRv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        timesRv.setAdapter(timesAdapter);

        timesAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                String value = timesAdapter.getItem(position).value;
                tvTime.setText(ConvertUtils.getRealNumValue(value, tvTime.getText().toString()));
            }
        });
    }


    public void getSummaryList() {
        JSONObject json = new JSONObject();
        try {
            json.put("mode_id",3);
            json.put("drug_id",drugParentTagId);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        RequestWithViewUtils.getInstance().addDisposable(RequestWithViewUtils.getInstance().apiServer.getSummaryList(body), new BaseObserver<BaseBean<List<AddDrugTag>>>(null, false) {
            @Override
            public void onSuccess(BaseBean<List<AddDrugTag>> o) {
                useAdapter.setNewInstance(o.result);
                useAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(String msg) {

            }
        });
    }


    public List<AddDrugTag> buildNumValue(){
        List<AddDrugTag> numList = new ArrayList<>();
        numList.add(new AddDrugTag("1",false));
        numList.add(new AddDrugTag("2",false));
        numList.add(new AddDrugTag("3",false));
        numList.add(new AddDrugTag("4",false));
        numList.add(new AddDrugTag("5",false));
        numList.add(new AddDrugTag("6",false));
        numList.add(new AddDrugTag("7",false));
        numList.add(new AddDrugTag("8",false));
        numList.add(new AddDrugTag("9",false));
        numList.add(new AddDrugTag("0",false));
        numList.add(new AddDrugTag("",false));
        numList.add(new AddDrugTag("清除",false));
        return numList;
    }
}
